package com.inei.eea2014.model.dao;

import java.util.ArrayList;
import java.util.List;

import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Ciiu;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CiiuDao implements ICRUD<Ciiu> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public CiiuDao(Context context) {
		util = new UtilDB(context);
	}

	// metodos ICRUD
	@Override
	public Ciiu buscar(Ciiu o) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ciiu> leer() {
		List<Ciiu> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Ciiu", null);
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Ciiu>();
			while (!cursor.isAfterLast()) {
				Ciiu obj = new Ciiu();
				obj.setCIIU(cursor.getString(cursor.getColumnIndex("CIIU")));
				obj.setNOMCIIU(cursor.getString(cursor
						.getColumnIndex("NOMCIIU")));
				lista.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(Ciiu o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Ciiu o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int eliminar(Ciiu o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
